package com.feinno.urcs.group.pd.servlet;

import com.feinno.msrp.MsrpSession;
import com.feinno.urcs.group.api.GroupService;
import com.feinno.urcs.group.pd.entity.GroupMessageArgs;
import com.feinno.urcs.nai.IMPU;
import com.feinno.urcs.sipservlet.LargeMessageServlet;
import com.feinno.urcs.sipservlet.SipServletRequest;
import com.feinno.urcs.sipservlet.msrp.MsrpSessionFactory;
import com.feinno.urcs.types.MobileNo;
import com.feinno.urcs.utils.SipHeaderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * Created by wuhao on 5/11/15.
 */
public class PdGroupLargeModeServlet extends LargeMessageServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(PdGroupFileTransferServlet.class);
    private GroupService groupService;
    private MsrpSessionFactory msrpFactory;
    private static final String FILE_KEY = "FILE_KEY";

    @Override
    protected OutputStream getOutputStream(SipServletRequest inviteRequest) {
        //获取发送者接受者
        IMPU sender = SipHeaderUtils.getFromUser(inviteRequest);
        IMPU grouper = SipHeaderUtils.getToUser(inviteRequest);
        ByteArrayOutputStream outputStream = null;

        try {
            //检查群是否存在及其他状态
            groupService.check( MobileNo.parseFrom(sender.getUser()), grouper.toUri().toString());
            //
            outputStream = new ByteArrayOutputStream();
            inviteRequest.getSession().put(FILE_KEY, outputStream);
        } catch (Exception e) {
            LOGGER.error("PdGroupLargeModeServlet check error {}", e);
        }
        LOGGER.info("PdGroupLargeModeServlet createOutputStream End");
        return outputStream;
    }

    @Override
    protected MsrpSession createMsrpSession(SipServletRequest sipServletRequest) {
        return msrpFactory.createSession();
    }

    @Override
    protected void event(Event event) {
        if (event.getState() == State.SUCCESS) {
            this.receiveDone(event.getInviteReuqest());
        } else if (event.getState() == State.FAILED) {
            LOGGER.error("", event.getError());
        }
    }

    protected void receiveDone(SipServletRequest sipServletRequest) {
        LOGGER.info("PdGroupLargeModeServlet receiveDone Begin");
        LOGGER.info("Received sipServletRequest:{}", sipServletRequest.toString());

        //构建
        GroupMessageArgs gma = new GroupMessageArgs();
        IMPU sender = SipHeaderUtils.getFromUser(sipServletRequest);
        IMPU grouper = SipHeaderUtils.getToUser(sipServletRequest);
        gma.setSender(sender);
        gma.setReceiver(grouper);

        //具体要?????
        BufferedReader br = new BufferedReader((Reader) sipServletRequest.getSession().get(FILE_KEY));
        StringBuilder sb = new StringBuilder();
        String  rline;
        try {
            rline = br.readLine();
            while(rline != null){
                sb.append(rline);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        gma.setBuffer(sb.toString().getBytes());

        //发送群消息
        //groupService.sendGroupMessage(gma);

        LOGGER.info("PdGroupLargeModeServlet receiveDone End");
    }
}
